package com.zty.xiaomi.server.Controller;


import com.zty.xiaomi.server.Entity.Insurance.Insurance;

import com.zty.xiaomi.server.Dto.GetOrderDetailInfoDto;
import com.zty.xiaomi.server.Dto.GetOrderGoodInfoDto;
import com.zty.xiaomi.server.Entity.Merchants.Merchant;
import com.zty.xiaomi.server.Entity.Order.*;
import com.zty.xiaomi.server.Entity.User;
import com.zty.xiaomi.server.Service.AlipayService;
import com.zty.xiaomi.server.Service.InsuranceOrder.InsuranceOrderServiceImp;
import com.zty.xiaomi.server.Service.Merchants.MerchantsServiceImpl;
import com.zty.xiaomi.server.Service.Order.OrderServiceImp;
import com.zty.xiaomi.server.Service.RegLogin.RegLogServiceImp;


import com.zty.xiaomi.server.Service.User.UserServiceImpl;
import com.zty.xiaomi.server.WebSocket.WebSocketServer;
import com.zty.xiaomi.server.WebSocket.takeGoodsSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("orders")
public class OrderController {

    @Autowired
    private OrderServiceImp orderServiceImp;

    @Autowired
    private MerchantsServiceImpl merchantsServiceImpl;
    @Autowired
    private RegLogServiceImp regLogServiceImp;

    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private takeGoodsSocket takeGoodsSocket;
    @Autowired
    private AlipayService alipayService;
    @Autowired
    private InsuranceOrderServiceImp insuranceOrderServiceImp;

    @Autowired
    private Alipay alipay;


//    @Autowired
//    private AlipayClient alipayClient;

    @PostMapping("/createorder")
    public OrderResult createOrder(@RequestBody OrdCreaParm ordCreaParm) throws Exception {
        OrderResult orderResult = new OrderResult();
        orderResult.setStatus(0);
        OrderList orderList = orderServiceImp.creatOrder(ordCreaParm);
        if (orderList.getOrderNo() == 0) {
            orderResult.setStatus(1);
            return orderResult;
        }
        orderResult.setData(orderList);
        return orderResult;
    }

    @RequestMapping("/getorderlist")
    public UserOrdListResult getUserOrdList(String username, String statusDesc) throws IOException {

        UserOrdListResult userOrdListResult = new UserOrdListResult();
        System.out.println(username);
        System.out.println(statusDesc);
        User user = regLogServiceImp.getUserByUserName(username);
        String userid = user.getUserid();

        List<UserOrdList> orderList = orderServiceImp.getOrderList(userid, statusDesc);

        for (UserOrdList userOrdList : orderList) {
            int orderNo = userOrdList.getOrderNo();
            Merchant merchantsMsgById = merchantsServiceImpl.getMerchantsMsgById(userOrdList.getStoreid());
            List<UserOrdItemList> orderListItems = orderServiceImp.getOrderListItems(orderNo);
            for (UserOrdItemList us : orderListItems
            ) {
                String insurance_id = us.getInsurance_id();
                List<Insurance> byId = insuranceOrderServiceImp.getInsuranceById(insurance_id);
                us.setInsuranceGoods(byId);

            }
            userOrdList.setMerchants(merchantsMsgById);
            userOrdList.setItems(orderListItems);
        }

        userOrdListResult.setStatus(0);
        userOrdListResult.setTotal(orderList.size());
        userOrdListResult.setList(orderList);

        return userOrdListResult;

    }


    //下单成功后调用下面两个方法
    @RequestMapping("/getorder")
    public OrdFinaResult getOrder(@RequestParam("id") int id) throws IOException {
        OrdFinaResult orderResult = new OrdFinaResult();
        orderResult.setStatus(0);
        OrdFina orderList = orderServiceImp.getOrderById(id);
        orderResult.setOrdFina(orderList);
        return orderResult;
    }


    @RequestMapping("/getorderdetail")
    public OrdFinaResult getOrderDetail(@RequestParam("username") String username) throws IOException {
        User user = regLogServiceImp.getUserByUserName(username);
        String userid = user.getUserid();
        List<orderItemVoList> orderItems = orderServiceImp.getOrderItems(userid);
        OrdFinaResult orderResult = new OrdFinaResult();
        orderResult.setStatus(0);
        orderResult.setLists(orderItems);
        return orderResult;
    }

    //    通过订单号查询订单详情
    @RequestMapping("/getOrderDetialByNo")
    public orderDetialResult getOrderDetialByNo(@RequestParam("id") int id) throws IOException {
        orderDetialResult orderDetialResult = new orderDetialResult();
        List<UserOrdItemList> orderListItems = orderServiceImp.getOrderListItems(id);
        for (UserOrdItemList us : orderListItems
        ) {
            List<Insurance> insuranceById = insuranceOrderServiceImp.getInsuranceById(us.getInsurance_id());
            us.setInsuranceGoods(insuranceById);
        }
        OrdFina ordFina = orderServiceImp.getOrderById(id);
        OrderList orderList = orderServiceImp.getOrderDetialByNo(id);
        orderDetialResult.setOrdFina(ordFina);
        orderDetialResult.setItemList(orderListItems);
        orderDetialResult.setOrderList(orderList);
        orderDetialResult.setCode(200);
        return orderDetialResult;
    }

    @RequestMapping("/buy")
    public void buyOrder(@RequestParam("id") int id,
                         @RequestParam("name") String username) {
        orderServiceImp.buyOrder(id, username);

    }

    @RequestMapping("/page")
    public String butrue(@RequestBody OrdPayParm ordPayParm) throws Exception {

        return alipayService.toPayPage(ordPayParm.getSubject(), ordPayParm.getOrderId(), ordPayParm.getTotal());
    }

    @GetMapping("getOrderDetailByOrderNo")
    private GetOrderDetailInfoDto getOrderDetailByOrderNo(@RequestParam("orderNo") int orderNo) throws IOException {
        GetOrderDetailInfoDto getOrderDetailInfoDto = new GetOrderDetailInfoDto();
        OrderDetail orderDetail = orderServiceImp.getOrderdetailByOrderNo(orderNo);
        if (orderDetail == null) {
            getOrderDetailInfoDto.setMsg("该订单下无数据！");
            getOrderDetailInfoDto.setStatus("400");
        } else {
            getOrderDetailInfoDto.setOrderDetail(orderDetail);
            getOrderDetailInfoDto.setStatus("200");
            OrdFina ordFina = orderServiceImp.getOrderById(orderNo);
            if (ordFina == null) {
                getOrderDetailInfoDto.setMsg("该订单下的用户部分数据丢失！");
                getOrderDetailInfoDto.setStatus("400");
            } else {
                getOrderDetailInfoDto.getOrderDetail().setOrdFina(ordFina);
                getOrderDetailInfoDto.setStatus("200");
            }
        }

        return getOrderDetailInfoDto;
    }

    @GetMapping("getOrderGoodByGoodIdAndOrderNo")
    private GetOrderGoodInfoDto getOrderGoodByGoodIdAndOrderNo(@RequestParam("orderNo") int orderNo,
                                                               @RequestParam("good_id") int good_id) throws IOException {
        GetOrderGoodInfoDto getOrderGoodInfoDto = new GetOrderGoodInfoDto();
        OrderGood orderGood = orderServiceImp.getOrderGoodByGoodIdAndOrderNo(good_id, orderNo);
        if (orderGood == null) {
            getOrderGoodInfoDto.setStatus("400");
            getOrderGoodInfoDto.setMsg("该订单下的商品信息丢失！");
        } else {
            getOrderGoodInfoDto.setData(orderGood);
            getOrderGoodInfoDto.setStatus("200");
        }
        return getOrderGoodInfoDto;
    }

    //确认收货
    @RequestMapping("/takeGoods")
    private int takseGoods(int orderNo) {
        orderServiceImp.UpdateOrder(4, "交易成功", orderNo);
        return 200;
    }

    //发货通知
    @RequestMapping("/ThedeliveryNotice")
    private int Thedeliverynotice(int orderNo) throws Exception {
        int thedeliverynotice = orderServiceImp.Thedeliverynotice(orderNo);
        OrderList orderDetialByNo = orderServiceImp.getOrderDetialByNo(orderNo);
        User userInfoById = userService.getUserInfoById(orderDetialByNo.getHostid());
        System.out.println(userInfoById.getName());
        takeGoodsSocket.send(userInfoById.getName()+"takeGoods", "您的" + orderNo + "商品已经发货");
        return thedeliverynotice;
    }


}
